package cn.spike.rockcc.web.controller;

import cn.spike.rockcc.domain.Course;
import cn.spike.rockcc.dto.CourseDto;
import cn.spike.rockcc.query.CourseQuery;
import cn.spike.rockcc.result.JSONResult;
import cn.spike.rockcc.result.PageList;
import cn.spike.rockcc.service.ICourseService;
import cn.spike.rockcc.vo.CourseDataVo;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;


    /**
     * 订单页面 - 获取订单信息
     * @param orderNo
     * @return
     */
    @GetMapping("/info/kill/{orderNo}")
    public JSONResult killInfo(@PathVariable("orderNo")String orderNo){
        CourseDataVo courseDataVo = courseService.killInfo(orderNo);
        return JSONResult.success(courseDataVo);
    }

    /**
     * 订单页面 - 获取订单信息
     * @param ids
     * @return
     */
    @GetMapping("/orderdata/{ids}")
    public JSONResult orderdata(@PathVariable("ids")String ids){
        CourseDataVo courseDataVo = courseService.orderdata(ids);
        return JSONResult.success(courseDataVo);
    }

    /**
     * 用户获取具体某个mediaUrl的接口
     * @param id
     * @return JSONResult
     */
    @GetMapping("/getForUser/{id}")
    public JSONResult getForUser(@PathVariable("id")Long id){
        String fileUrl = courseService.getForUser(id);
        return JSONResult.success(fileUrl);
    }

    /**
     * 点击课程详情的接口
     * @param id
     * @return
     */
    @GetMapping("detail/data/{id}")
    public JSONResult getCourse(@PathVariable("id")Long id){
        return JSONResult.success(courseService.getCourse(id));
    }


    /**
     * 课程发布的接口
     * @param id
     * @return
     */
    @PostMapping("/onLineCourse/{id}")
    public JSONResult onLineCourse(@PathVariable("id")Long id){
        courseService.onLineCourse(id);
        return JSONResult.success();
    }

    /**
     * 新增课程
     * @param courseDto
     */
    @PostMapping("/save")
    public JSONResult save(@RequestBody CourseDto courseDto){
        courseService.save(courseDto);
        return JSONResult.success();
    }

    /**
    * 保存和修改公用的
    */
    /*@RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody Course course){
        if(course.getId()!=null){
            courseService.updateById(course);
        }else{
            courseService.insert(course);
        }
        return JSONResult.success();
    }*/

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(courseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(courseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        Wrapper<Course> wrapper = new EntityWrapper<>();
        wrapper.like("name",query.getKeyword());
        page = courseService.selectPage(page,wrapper);
        return JSONResult.success(new PageList<Course>(page.getTotal(),page.getRecords()));
    }
}
